﻿using Autofac;
using CSRedis;
using SH.Common;
using SH.Common.CommonClass;

using SH.Multitenant.Core.Saas;
using SH.Multitenant.Domain.RBAC;
using SH.Multitenant.Infrastructure.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SH.Multitenant.Infrastructure.Repository
{
    public class StoreRepository : BaseRepository<Store>, IStoreRepository
    {
        private readonly ISqlSugarClient _context;
        private readonly CSRedisClient _client = AppService.AutofacContainer.Resolve<CSRedisClient>();
        public StoreRepository(ISqlSugarClient context) : base(DbManger.BizDb)
        {
            _context = DbManger.BizDb;
        }
        
        //重写属性
        public override ISugarQueryable<Store> All => _context.AsTenant().QueryableWithAttr<Store>().Where(x => x.TenantId == _client.Get<string>("tenantId"));
        //获取门店列表
        public async Task<List<Store>> GetStoreListAsync()
        {
            var list = _client.Get<List<Store>>("storeSelectList");
            if (list != null && list.Count() > 0)
            {
                return list;
            }
            list = await _context.AsTenant().QueryableWithAttr<Store>().Select(x => new Store
            {
                StoreId = x.StoreId,
                StoreName = x.StoreName,
            }).ToListAsync();
            _client.Set("storeSelectList", list);
            return list;
        }
        //重写添加
        public async override Task<int> InsertAsync(Store store)
        {
            var result = await _context.AsTenant().InsertableWithAttr<Store>(store).ExecuteCommandAsync();
            return result;
        }
    }
}
